home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: MegaDisc / MegaDisc 27 (1992-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).zip / MegaDisc 27 (1992-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).adf / Programming / Integer_Math / Int32.i < prev    next >
Text File  |  1992-03-30  |  4KB  |  142 lines

  1.     IFND    INT32VER
  2.     ;------------------------------------------------------------------
  3.     ; Created by Peter Thompson
  4.     ; Multiply/square routines written on New Year's Day 1992
  5.     ; & debugged on 2nd January, when I was feeling better....
  6.     ; Division macros written & partially debugged on 2nd Jan.
  7.     ; Further testing and debugging on 9th of Jan.
  8.     ;------------------------------------------------------------------
  9.     ; $MUSIC="Vince Jones/It all ends up in tears, Enya/Watermark,
  10.     ;         Paul Simon/Graceland, Clannad/Fuaim, Phil Keaggy/Sunday's
  11.     ;      Child, Phil Keaggy/Master and the Musician, Farrell &
  12.     ;      Farrell/Superpower"
  13.     ;------------------------------------------------------------------
  14.     ;  Numbers are all integers.
  15.     ;  Only registers containing results, and/or d1, are altered by
  16.     ; these macros.
  17.     ;  Macros may require up to 80 bytes of stack space.
  18.     ; Flag    State after any multiply/square macro
  19.     ; X    undefined.
  20.     ; N    Set if result is negative, clear otherwise.
  21.     ; Z    Set if result is zero, clear otherwise.
  22.     ; V    if set, an overflow HAS taken place;
  23.     ;    if clear, an overflow MAY have taken place.
  24.     ; C    undefined.
  25.     ;  None of these routines can be trusted to return a correct
  26.     ; overflow flag, unless specified by name to do so. It is your
  27.     ; responsiblity to make sure your numbers are small enoungh.
  28.     ;------------------------------------------------------------------
  29.  
  30. INT32VER    EQU    100
  31.  
  32.     ;- MUL32 macro - general 32 bit multiply.
  33.     ;- Input:    d0 contains a 32-bit signed or unsigned number.
  34.     ;-        d1 contains a 32-bit signed or unsigned number.
  35.     ;- Output:    d0 contains the product of the inputs.
  36.     ;- Alters:    d1
  37.  
  38. MUL32:    MACRO            ; d0 d1 d2 d3
  39.     movem.l    d2-d3,-(SP)    ; ab cd -- --
  40.     move.l    d1,d2        ; ab cd cd --
  41.     swap    d1        ; ab dc cd --
  42.     move.l    d0,d3        ; ab dc cd ab
  43.     swap    d3        ; ab dc cd ba
  44.     mulu    d0,d1        ; ab BC cd ba
  45.     mulu    d2,d3        ; ab BC cd AD
  46.     mulu    d2,d0        ; BD BC cd AD
  47.     add.l    d1,d3        ; BD BC cd 12
  48.     swap    d3        ; BD BC cd 21
  49.     clr.w    d3        ; BD BC cd 20
  50.     add.l    d3,d0        ; ++ BC cd 20
  51.     movem.l    (SP)+,d2-d3    ; ++ BC -- --
  52.     ENDM
  53.  
  54.     ;- SSQR32 macro - signed 32-bit square.
  55.     ;- Input:    d0 contains a 32-bit signed number.
  56.     ;- Output:    d0 contains the square of the input.
  57.  
  58. SSQR32:    MACRO
  59.     tst.l    d0
  60.     bmi.S    *+4    ;->ss32a
  61.     neg.l    d0
  62.     mulu    d0,d0    ;ss32a:
  63.     ENDM
  64.  
  65.     ;- USQR32 macro - unsigned 32-bit square.
  66.     ;- Input:    d0 contains a 32-bit unsigned number.
  67.     ;- Output:    d0 contains the square of the input.
  68.  
  69. USQR32:    MACRO
  70.     mulu    d0,d0
  71.     ENDM
  72.  
  73.     ;- GSQR32 macro - general 32-bit square.
  74.     ;- Input:    d0 contains a 32-bit signed or unsigned number.
  75.     ;- Output:    d0 contains the square of the input.
  76.     ;- Alters:    d1
  77.  
  78. GSQR32:    MACRO            ; d0 d1
  79.     move.l    d0,d1    
  80.     swap    d1        ; ab ba
  81.     mulu    d0,d1        ; ab AB
  82.     mulu    d0,d0        ; BB AB
  83.     swap    d1
  84.     clr.w    d1        ; BB B0
  85.     add.l    d1,d1
  86.     add.l    d1,d0
  87.     ENDM
  88.  
  89.     ;- UDIV32 macro - 32-bit unsigned division.
  90.     ;- Input:    d0 contains the 32-bit unsigned dividend.
  91.     ;-        d1 contains the 32-bit unsigned divisor.
  92.     ;- Output:    d0 contains the 32-bit unsigned quotient.
  93.     ;-        d1 contains the 32-bit unsigned remainder.
  94.  
  95. UDIV32:    MACRO
  96.     movem.l    d2-d3,-(SP)
  97.     moveq    #0,d2
  98.     moveq    #31,d3
  99.     lsl.l    #1,d0
  100.     roxl.l    #1,d2        ;ud32a:
  101.     sub.l    d1,d2
  102.     bcc.S    *+4        ;->ud32b
  103.     add.l    d1,d2
  104.     roxl.l    #1,d0        ;ud32b:
  105.     dbra    d3,*-10        ;->ud32a
  106.     not.l    d0
  107.     move.l    d2,d1
  108.     movem.l    (SP)+,d2-d3
  109.     ENDM
  110.  
  111.     ;- SDIV32 macro - 32-bit signed division.
  112.     ;- Input:    d0 contains the 32-bit signed dividend.
  113.     ;-        d1 contains the 32-bit signed divisor.
  114.     ;- Output:    d0 contains the 32-bit signed quotient.
  115.     ;-        d1 contains the 32-bit signed remainder.
  116.     ;- Let a be the dividend, b the divisor, c the quotient and d the
  117.     ;- remainder. Then both of the following hold:
  118.     ;-         (1) a = b*c+d
  119.     ;-        (2) abs(a) = abs(b)*abs(c)+abs(d)
  120.  
  121. SDIV32:    MACRO
  122.     movem.l    d2-d3,-(SP)
  123.     move.l    d0,d2
  124.     eor.l    d1,d2
  125.     move.l    d0,d3
  126.     bpl.S    *+4        ;->sd32a
  127.     neg.l    d0
  128.     tst.l    d1        ;sd32a:
  129.     bpl.S    *+4        ;->sd32b
  130.     neg.l    d1
  131.     UDIV32            ;sd32b:
  132.     tst.l    d3
  133.     bpl.S    *+4        ;->sd32c:
  134.     neg.l    d1
  135.     tst.l    d2        ;sd32c:
  136.     bpl.S    *+4        ;->sd32d
  137.     neg.l    d0
  138.     movem.l    (SP)+,d2-d3    ;sd32d:
  139.     ENDM
  140.  
  141.     ENDC
  142.